package com.jydd.liaomingframe.main;

import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.jydd.liaomingframe.R;
import com.jydd.liaomingframe.main.fragment.FragmentMe;
import com.jydd.liaomingframe.main.fragment.FragmentModuleOne;
import com.jydd.liaomingframe.main.fragment.FragmentModuleTwo;
import com.jydd.liaomingframe.main.fragment.WorkFragment;
import com.lm.liaoming.base.custom.CustomBaseActivity;
import com.lm.liaoming.tool.StatesUtils;
import com.lm.liaoming.widght.NoScrollViewPager;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;
import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends CustomBaseActivity {
    @BindView(R.id.tabs)
    CommonTabLayout tabs;
    @BindView(R.id.viewPager)
    NoScrollViewPager mViewPager;
    // tab选中图标集合
    private List<Integer> selectedIconRes = new ArrayList<>();
    // tab未选中图标集合
    private List<Integer> unSelectIconRes = new ArrayList<>();
    // tab标题集合
    private List<String> titleRes = new ArrayList<>();
    // CommonTabLayout所需数据集合
    private List<CustomTabEntity> data = new ArrayList<>();
    // fragment集合
    private List<Fragment> fsRes = new ArrayList<>();
    @Override
    public int setLayoutId() {
        StatesUtils.setStatusBar(this);
        return R.layout.activity_main;
    }
    @Override
    public void initVariables() {

    }

    @Override
    public void initViews() {
        ButterKnife.bind(this);
        // 初始化数据
        initData();
        // 设置监听
        initListener();
    }

    private void initListener() {
        // TabLayout监听
        tabs.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                // 显示相应的item界面
                mViewPager.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
        // ViewPager监听
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                // 设置相应选中图标和颜色
                tabs.setCurrentTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        // 设置默认第0个
        mViewPager.setCurrentItem(0);
    }

    private void initData() {
        mViewPager.setNoScroll(true);
        // 图片选中资源
        selectedIconRes.add(R.mipmap.tab_home_select);
        selectedIconRes.add(R.mipmap.tab_speech_select);
        selectedIconRes.add(R.mipmap.tab_contact_select);
        selectedIconRes.add(R.mipmap.tab_more_select);
        // 图片未选中资源
        unSelectIconRes.add(R.mipmap.tab_home_unselect);
        unSelectIconRes.add(R.mipmap.tab_speech_unselect);
        unSelectIconRes.add(R.mipmap.tab_contact_unselect);
        unSelectIconRes.add(R.mipmap.tab_more_unselect);
        // 标题资源
        titleRes.add("拆前");
        titleRes.add("拆中");
        titleRes.add("拆后");
        titleRes.add("我的");
        // fragment数据
        fsRes.add(WorkFragment.newInstance());
        fsRes.add(FragmentModuleOne.newInstance());
        fsRes.add(FragmentModuleTwo.newInstance());
        fsRes.add(FragmentMe.newInstance());
        // 设置数据
        for(int i=0; i<titleRes.size(); i++){
            int index = i;
            data.add(new CustomTabEntity() {
                @Override
                public String getTabTitle() {
                    return titleRes.get(index);
                }

                @Override
                public int getTabSelectedIcon() {
                    return selectedIconRes.get(index);
                }

                @Override
                public int getTabUnselectedIcon() {
                    return unSelectIconRes.get(index);
                }
            });
        }
        // 设置数据
        tabs.setTabData((ArrayList<CustomTabEntity>) data);
        mViewPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager(), FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT));
    }
    private class MyPagerAdapter extends FragmentPagerAdapter {

        public MyPagerAdapter(@NonNull FragmentManager fm, int behavior) {
            super(fm, behavior);
        }

        @NonNull
        @Override
        public Fragment getItem(int position) {
            return fsRes.get(position);
        }

        @Override
        public int getCount() {
            return fsRes.size();
        }
    }
}
